//
//  CZStatusForwardCell.swift
//  GZWeibo007
//
//  Created by Apple on 15/12/21.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SnapKit
/// 转发cell
class CZStatusForwardCell: CZStatusCell {
    
    override var status: CZStatus? {
        didSet {
            
            switch picCount{
                
            case 0 : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.height.equalTo(0)

                
            })
            case 1 : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView).offset(8)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.height.equalTo(120).priorityHigh()
                make.width.equalTo(90).priorityHigh()
            })
            case 4 : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView).offset(8)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.width.equalTo(190).priorityHigh()
                make.height.equalTo(190).priorityHigh()
            })
            case 2,3 : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView).offset(8)
                make.trailing.equalTo(contentView)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.height.equalTo(90).priorityHigh()
            })
                
            case 5,6 : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView).offset(8)
                make.trailing.equalTo(contentView)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.height.equalTo(180).priorityHigh()
            })
            default : pictureView.snp_remakeConstraints(closure: { (make) -> Void in
                make.leading.equalTo(contentView).offset(8)
                make.trailing.equalTo(contentView)
                make.top.equalTo(forwardLabel.snp_bottom).offset(8)
                make.height.equalTo(290).priorityHigh()
            })
                
                
            }
            
            // 设置被转发微博的文本内容
            // 获取被转发微博模型
            if let forwardStatus = status?.retweeted_status {
                let text = "@\(forwardStatus.user!.screen_name!): \(forwardStatus.text!)"
                forwardLabel.text = text
            }
        }
    }
    
    // 覆盖父类的方法
    override func preapreUI() {
        super.preapreUI()
        
        // 添加子控件
        contentView.addSubview(bkgButton)
        contentView.addSubview(forwardLabel)
        // 将背景按钮发送到最底部
        contentView.sendSubviewToBack(bkgButton)
        
        
        
        // 添加约束
        forwardLabel.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(3)
        }
        
        bkgButton.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(forwardLabel)
            make.bottom.equalTo(bottomView.snp_top).offset(-3)
        }
        
        bottomView.snp_remakeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(contentView)
            //            make.height.equalTo(44)
            make.top.equalTo(pictureView.snp_bottom).offset(8)
            make.height.equalTo(36)
        }
        
        
        
    }
    
    // MARK: - 懒加载
    /// 背景
    lazy var bkgButton: UIButton = {
        let button = UIButton()
        
        // 设置背景颜色
        button.backgroundColor = UIColor(white: 0.88, alpha: 1)
        button .addTarget(self , action: "forWardBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    var value: ((status: CZStatus) -> ())?
    
    func forWardBtnClick () {
        if (value != nil) {
            value!(status: status!.retweeted_status!)
        }
    }
    
    /// 转发微博的文本内容
    private lazy var forwardLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        label.sizeToFit()
        // 设置显示多行
        label.numberOfLines = 0
        
        // 设置折行文本宽度(换行的宽度)
        label.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        
        return label
    }()
}
